<?php

namespace App\Listeners\CollectPackage;

use App\Common\Libs\Dispatch\Console;
use App\Common\Libs\Dispatch\Queue;
use App\Console\Commands\Controller\PackageCommand;
use App\Consts\GlobalConsoleConst;
use App\Consts\GlobalQueueConst;
use App\Events\CollectPackage\DelCollectPackageEvent;
use App\Jobs\Controller\PackageJob;
use App\Listeners\BaseListener;

class DelCollectPackageListener extends BaseListener
{
    /**
     * Handle the event.
     *
     * @param DelCollectPackageEvent $event
     * @return bool
     */
    public function handle(DelCollectPackageEvent $event)
    {
        $this->saveFileLog(__CLASS__, $event);

        $collectPackage = $event->collectPackage;

        // 更新收藏内容的收藏数 -1，记录取消收藏日志
        Console::getInstance(PackageCommand::class)
            ->push([
                'user_id' => $collectPackage['user_id'],
                'collect_package_id' => $collectPackage['collect_package_id'],
            ], GlobalConsoleConst::PACKAGE_SYNC_TARGET_COUNT);

        // 队列记录日志
        service()->LogCountService->createLogByQueue($event->logCountLoadData);

        // 取消所有内容的收藏态【等于取消收藏的一切操作】
        Queue::getInstance(PackageJob::class)
            ->push([
                'user_id' => $collectPackage['user_id'],
                'package_id' => $collectPackage['collect_package_id'],
            ], GlobalQueueConst::PACKAGE_DEL_PACKAGE);

        return true;
    }
}
